package edu.xmut.kakamusic.fragment.video;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Arrays;
import java.util.List;

import edu.xmut.kakamusic.R;
import edu.xmut.kakamusic.adapter.video.VideoListAdapter;

public class VideoListFragment extends Fragment {

    private List<String> list;
    private VideoDetailFragment videoDetailFragment;
    private final String url0 = "http://47.115.139.249:8000/v1.mp4";
    private final String url1 = "http://47.115.139.249:8000/v2.mp4";
    private final String url2 = "http://47.115.139.249:8000/v3.mp4";
    private final String url3 = "http://47.115.139.249:8000/v1.mp4";
    private final String url4 = "http://47.115.139.249:8000/v2.mp4";
    private final String url5 = "http://47.115.139.249:8000/v3.mp4";
    private final String url6 = "http://47.115.139.249:8000/v2.mp4";
    private final String url7 = "http://47.115.139.249:8000/v1.mp4";

    public VideoListFragment(String[] list, VideoDetailFragment videoDetailFragment) {
        this.list = Arrays.asList(list);
        this.videoDetailFragment = videoDetailFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_video_list, container, false);
        RecyclerView recyclerView = root.findViewById(R.id.recyclerview_video_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerItemDecoration(root.getContext(),
                DividerItemDecoration.VERTICAL));
        VideoListAdapter videoListAdapter = new VideoListAdapter(list);
        recyclerView.setAdapter(videoListAdapter);
        videoListAdapter.setOnItemClickListener((adapter, view, position) -> {
            switch (position) {
                case 0:
                    videoDetailFragment.playNewUrl(url0);
                    break;
                case 1:
                    videoDetailFragment.playNewUrl(url1);
                    break;
                case 2:
                    videoDetailFragment.playNewUrl(url2);
                    break;
                case 3:
                    videoDetailFragment.playNewUrl(url3);
                    break;
                case 4:
                    videoDetailFragment.playNewUrl(url4);
                    break;
                case 5:
                    videoDetailFragment.playNewUrl(url5);
                    break;
                case 6:
                    videoDetailFragment.playNewUrl(url6);
                    break;
                case 7:
                    videoDetailFragment.playNewUrl(url7);
                    break;
            }
        });

        return root;
    }
}
